import { SYSTEM_CONFIG } from "@/config/system";
import service from "@/api";

const MINIO_UPLOAD_ATTACHMENT_SERVICE = "/minio/UploadAttachmentService";
const MINIO_DOWNLOAD_ATTACHMENT_SERVICE = "/minio/DownloadAttachmentService";

export const downloadMInioFileUrl = SYSTEM_CONFIG.BASE_URL + MINIO_DOWNLOAD_ATTACHMENT_SERVICE + "/downloadFile?fileId=";

export type MinioAttachmentBean = {
  /**应用id */
  appId: string;
  /**附件id */
  attachmentId: string;
  /**储存桶 */
  bucketName: string;
  /**后缀名 */
  fileExt: string;
  /**文件路径 */
  filePath: string;
  /**文件大小 */
  fileSize: number;
  /**mime */
  mimeType: string;
  createPerson: string;
  createTime: string;
  modifyPerson: string;
  modifyTime: string;
  orgId: string;
  originalName: string;
  subOrgId: string;
};

/**
 *
 * @param bucketName 储存桶名称
 * @param objectName 对象名称
 */
export function getUploadObjectUrl(bucketName: string, objectName: string) {
  return service.get(MINIO_UPLOAD_ATTACHMENT_SERVICE + "/getUploadObjectUrl", {
    params: { bucketName, objectName }
  });
}

/**
 *
 * @param bucketName 储存桶名称
 * @param objectName 对象名称
 * @param partCount 分片个数
 */
export function initMultiPartUpload(bucketName: string, objectName: string, partCount: number) {
  return service.get(MINIO_UPLOAD_ATTACHMENT_SERVICE + "/initMultiPartUpload", {
    params: { bucketName, objectName, partCount }
  });
}

/**
 *
 * @param bucketName 储存桶名称
 * @param uploadId 上传id
 * @param objectName 对象名称
 * @param partCount 分片个数
 * @param originalName 文件原名称
 * @param mimeType mime类型
 * @param fileSize 文件大小
 */
export function mergeMultiPartUpload(
  bucketName: string,
  uploadId: string,
  objectName: string,
  partCount: number,
  originalName: string,
  mimeType: string,
  fileSize: number
) {
  return service.get(MINIO_UPLOAD_ATTACHMENT_SERVICE + "/mergeMultiPartUpload", {
    params: { bucketName, uploadId, objectName, partCount, originalName, mimeType, fileSize }
  });
}

/**
 *
 * @param bucketName 储存桶名称
 * @param objectName 对象名称
 * @param originalName 文件原名称
 * @param mimeType mime类型
 * @param fileSize 文件大小
 */
export function insertAttachmentByObjectName(
  bucketName: string,
  objectName: string,
  originalName: string,
  mimeType: string,
  fileSize: number
) {
  return service.get(MINIO_UPLOAD_ATTACHMENT_SERVICE + "/insertAttachmentByObjectName", {
    params: { bucketName, objectName, originalName, mimeType, fileSize }
  });
}

/**
 * 下载minio附件
 * @param fileId 文件id
 */
export function downloadMinioFile(fileId: string) {
  return service.get(MINIO_DOWNLOAD_ATTACHMENT_SERVICE + "/downloadFile?fileId=" + fileId, {
    responseType: "blob",
    timeout: SYSTEM_CONFIG.DOWNLOAD_FILE_TIMEOUT
  });
}
/**
 * 保存附件
 * @param fileId 文件id
 * @param compId 组件id
 * @param itemType 类型
 * @param busiId 业务id
 * @param projectId 电站id
 * @param originalName 文件原名称
 * @param fileExt 文件后缀名
 * @param mimeType mime
 * @param attachmentSize 文件大小
 * @returns
 */
export function saveMulitFiles(
  fileId: string,
  compId: string,
  itemType: string,
  busiId: string,
  projectId: string | number,
  originalName: string,
  fileExt: string,
  mimeType: string,
  attachmentSize: number,
  typeid: string | number
) {
  return service.post(MINIO_UPLOAD_ATTACHMENT_SERVICE + "/saveMulitFiles", {
    fileId,
    compId,
    itemType,
    busiId,
    projectId,
    originalName,
    fileExt,
    mimeType,
    attachmentSize,
    typeid
  });
}
